package demo.jdk8;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.function.Consumer;

import org.junit.Test;

public class TimeZoneTest {
	public static Consumer<Object>  print = System.out::println;
	

	@Test
	 public void testTimezone() {
		print.accept(ZoneId.getAvailableZoneIds());
		ZoneId zone1 = ZoneId.of("Europe/Berlin");
		ZoneId zone2 = ZoneId.of("Asia/Shanghai");
		print.accept(zone1.getRules());
		print.accept(zone2.getRules());
		
		LocalTime now1 = LocalTime.now(zone1);
		LocalTime now2 = LocalTime.now(zone2);
		print.accept(now1.isBefore(now2));  // true
		long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
		long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
		print.accept(hoursBetween);       // 6
		print.accept(minutesBetween);     // 360
		
		LocalTime late = LocalTime.of(23, 59, 59);
		print.accept(late);       // 23:59:59

		DateTimeFormatter germanFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
		        .withLocale(Locale.GERMAN);
		LocalTime leetTime = LocalTime.parse("13:37", germanFormatter);
		print.accept(leetTime);   // 13:37
	 }
	
	@Test
    public  void timezoneGet() {
		//就API获取时区--可行
		SimpleDateFormat sdf = new SimpleDateFormat("z");  
        long times = System.currentTimeMillis();  
        System.out.println(sdf.format(times));  
		
        String timezone = "Asia/Shanghai";
        System.setProperty("user.timezone", timezone);
        TimeZone.setDefault(TimeZone.getTimeZone(timezone));
//		Calendar cal = Calendar.getInstance();
		TimeZone timeZone = TimeZone.getDefault();//cal.getTimeZone();
		System.out.println(timeZone.getID());
		System.out.println(timeZone.getDisplayName(Locale.US));
		System.out.println(timeZone.getDisplayName(Locale.US));
		//新API获取时区--可行
		System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));
		final DateTimeFormatter outputFormat1 = DateTimeFormatter.ofPattern("yyyyMMdd zzz", Locale.CHINA);
        //新API获取时区
        String strdate ="05/Sep/2016:17:02:49 +0800";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH) ;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(strdate, formatter);
        System.out.println(zonedDateTime.toEpochSecond());
	      
		
		
        // The test string
        String str = "Sun Oct 19 01:00:00 ADT 2014";

        // Formatter for the input date
        final DateTimeFormatter inputFormat =
                DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");

        // The parsed date
        final LocalDateTime parsed = LocalDateTime.parse(str, inputFormat);

        // The output format(s). Specify the one you need
        final DateTimeFormatter outputFormat2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // Print
        System.out.println(outputFormat1.format(parsed)); // -> 2014/10/21
        System.out.println(outputFormat2.format(parsed)); // -> 2014-10-21
    }
    
}
